package me.algo.tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Objects;
import java.util.StringJoiner;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.joining;

/**
 * @author guozheng
 * @date 2024/4/18
 */
public class T6 {
    public static void main(String[] args) {
        StringJoiner stringJoiner = new StringJoiner("->");
        stringJoiner.add("1");
        stringJoiner.add("2");
        stringJoiner.add("4");
        String string = stringJoiner.toString();
        System.out.println(string);

        List<Integer> list = new LinkedList<>();
        list.addLast(1);
        list.addLast(1);
        list.stream().map(Object::toString).collect(Collectors.joining("->"));

    }



    /**
     * 257. 二叉树的所有路径
     * @param root
     * @return
     */
    public static List<String> binaryTreePaths(TreeNode root) {
        if (Objects.isNull(root)) {
            return new ArrayList<>();
        }
        LinkedList<Pair> que = new LinkedList<>();
        Pair pair = new Pair(root, new ArrayList<>());
        que.addLast(pair);
        List<String> rtn = new ArrayList<>();

        while (!que.isEmpty()) {
            int size = que.size();
            while (size > 0) {
                size--;
                Pair p = que.removeFirst();
                TreeNode node = p.node;
                if (node.right == null && node.left == null) {
                    List<Integer> valList = p.valList;
                    List<Integer> list = new LinkedList<>(valList);
                    list.addLast(node.val);
                    String sv = list.stream().map(Objects::toString).collect(Collectors.joining("->"));
                    rtn.add(sv);
                    continue;
                }
                if (node.left != null) {
                    List<Integer> valList = p.valList;
                    List<Integer> next = new LinkedList<>(valList);
                    next.addLast(node.val);
                    que.addLast(new Pair(node.left, next));
                }
                if (node.right != null) {
                    List<Integer> valList = p.valList;
                    List<Integer> next = new LinkedList<>(valList);
                    next.addLast(node.val);
                    que.addLast(new Pair(node.right, next));
                }
            }
        }
        return rtn;
    }

    record Pair(TreeNode node, List<Integer> valList) {

    }
}
